<template>
    <div>
        <div style="padding: 1px 0">
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-present"
            placeholder="请输入鲜花分类名称"
            v-model="name"
          ></el-input>
          <el-button class="ml-5" type="primary" @click="load"  style="font-size:medium">搜索</el-button>
          <el-button class="ml-5" type="warning" @click="reset"  style="font-size:medium">重置</el-button>
        </div>

        <div style="padding: 5px 0">
          <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd" style="font-size:medium">添加</el-button>
          <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
          <el-button type="danger" slot="reference" style="font-size:medium">批量删除</el-button>
        </el-popconfirm>
          <el-button class="ml-5" type="primary" @click="exp" style="font-size:medium">导出</el-button>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg3'" @selection-change="handleSelectionChange" style="font-size:large">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            prop="id"
            label="ID"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="鲜花分类名称"
            width="160"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="sort"
            label="排序"
            width="90"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="popular"
            label="受欢迎程度"
            width="130"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="256"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="createUser"
            label="创建人"
            width="110"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" width="280" align="center">
            <template slot-scope="scope">
              <el-button type="success"
              @click="handleEdit(scope.row)"
                >编辑 <i class="el-icon-edit"></i
              ></el-button>

              <el-popconfirm
                class="ml-5"
                confirm-button-text='确定删除'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要删除吗?"
                @confirm="handleDel(scope.row.id)"
                >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[7, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

        <el-dialog title="分类信息" :visible.sync="dialogFormVisible" width="35%">
          <el-form label-width="100px" size="small">
            <el-form-item label="分类名称：">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="分类顺序：">
              <el-input v-model="form.sort" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="受欢迎程度：">
              <el-input v-model="form.popular" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Category',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        name: "",
        dialogFormVisible: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
    }
  },
  created(){
    this.load();
  },
  methods:{
    load() {
        this.request.get("/category/page",{
          params :{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
          }
        }).then(res =>{
          this.tableData = res.records
          this.total = res.total
        })
      },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
       this.name = "",
       this.code = "",
       this.load()
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/category/delete/"+id).then(res =>{
        if(res){
          this.$message.success('删除鲜花分类成功!')
          this.load()
        }else{
          this.$message.error('删除鲜花分类失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除鲜花分类前,请先勾选若干分类')
      }else{
        this.request.delete("/category/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除鲜花分类成功!')
            this.load()
          }else{
            this.$message.error('批量删除鲜花分类失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/category/save",this.form).then(res =>{
        console.log(res)
        if(res){
          this.$message.success('添加鲜花分类成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error('添加鲜花分类失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/category/edit",this.form).then((res) =>{
          if(res){
            this.$message.success("修改鲜花信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改鲜花信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
    exp(){
      window.open("http://8.138.58.18:7777/category/export")
    },
  }
};
</script>

<style>
.headerBg3 {
  background-color: rgb(143, 230, 194) !important;
}
</style>